<?php
namespace app\admin\controller\customized;
use app\admin\controller\Base;
use app\BaseController;
use think\facade\Db;
class Ad extends Base{
    public function list_data(){
        $listData = Db::table('tb_ad')->select()->toArray(); 
        foreach($listData as $k=>$v){
            $listData[$k]['imgs'] = unserialize($v['imgs']);
            $listData[$k]['up_time'] = date("Y-m-d G:i",$v['up_time']);
        }
        $this->assign('listData',json_encode($listData));
        return $this->fetch();
    
    }

    //编辑数据
    public function form_data($id=0,$type){
        $pt = input("post.");
        if(count($pt)>0){
            if($id>0){
                //修改
                $pt['up_time'] = time();
                if (!empty($pt['imgs'])) {
                    $pt['imgs'] =  serialize($pt['imgs']);
                }else{
                    $pt['imgs'] = serialize([]);
                }
                $bool = Db::table('tb_ad')->where('id', $pt['id'])->data($pt)->update();
                $this->logData('修改产品:'.$pt['title']);
            }else{
                //新增
                $pt['create_time'] = time();
                $pt['up_time'] = time();
                $pt['imgs'] =  serialize($pt['imgs']);
                $bool = Db::table('tb_ad')->insert($pt);
                $this->logData('新增产品:'.$pt['title']);
            }
            if($bool == 1){
                $this->info = ['code'=>1,'msg'=>'操作成功','data'=>$pt];
            }else{
                $this->info = ['code'=>0,'msg'=>'操作失败','data'=>$pt];
            }
            return json($this->info);
        }
        if($id!=0){
            $pageData = Db::table('tb_ad')->where('id', $id)->find();
            $pageData['imgs'] = unserialize($pageData['imgs']);
        }else{
            $pageData = [
                'title'=>'',
                'notes'=>'',
                'imgs'=>[],
                'img'=>[],
                'type'=>'',
                'is_show'=>1,
            ];
        }
        $this->assign('pageDataJson',json_encode($pageData));
        $this->assign('pageData',$pageData);
        $typeList = Db::query("select * from an_type where type=1");
        $this->assign('typeList',$typeList);
        $this->assign('id',$id);
        $this->assign('type',$type);
        return $this->fetch();
    }
}